﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Models.ViewDataModels;
using MvcShop.Models.Repositories;
using System.Data.Objects;

namespace MvcShop.Models.Services
{
    public interface IProductsService
    {
        IEnumerable<PPCModel> GetPPCModel(int productId);
        IEnumerable<ProductsToRemarkModel> GetListPTR(int page, int pageSize);
        IEnumerable<ProductsToRemarkModel> GetUserPTR(string userName, int remType, int page, int pageSize);
        IEnumerable<FProductList> GetProByKeyWord(ICriteria criteria, int page, int pageSize, Order orderExpr);
        IEnumerable<ProductList> GetProductList(ICriteria criteria, int page, int pageSize, Order orderExpr);
    }

    public class ProductsService : ServiceBase,IProductsService
    {
        #region IProductsService 成员

        public IEnumerable<PPCModel> GetPPCModel(int productId)
        {
            var query = from ppc in this.dataContext.Product_propertys_contents
                        join pps in this.dataContext.Product_properties
                        on ppc.PropertyId equals pps.Product_propertyId
                        where ppc.ProductId == productId
                        select new PPCModel
                        {
                            Product_propertys_contentId = ppc.Product_propertys_contentId,
                            ProductId = ppc.ProductId,
                            PropertyId = ppc.PropertyId,
                            PropertyName = pps.PropertyName,
                            PropertyContent = ppc.PropertyContent
                        };

            return query.ToList();
        }

        public IEnumerable<ProductsToRemarkModel> GetListPTR(int page, int pageSize)
        {
            var query = (from rem in this.dataContext.Remarks
                        join prod in this.dataContext.Products
                        on rem.ProductId equals prod.ProductId
                        where rem.RemarkType == 3 || rem.IsHandle == false
                        orderby rem.RemarkId descending
                        select new ProductsToRemarkModel
                        {
                            RemarkId = rem.RemarkId,
                            ProductId = rem.ProductId,
                            ProductName = prod.ProductName,
                            UserName = rem.UserName,
                            Body = rem.Body,
                            DateTime = rem.DateTime
                        }).Skip((page - 1) * pageSize).Take(pageSize).ToList();

            return query;
        }

        public IEnumerable<ProductsToRemarkModel> GetUserPTR(string userName,int remType, int page, int pageSize)
        {
            var query = (from rem in this.dataContext.Remarks
                         join prod in this.dataContext.Products
                         on rem.ProductId equals prod.ProductId
                         where rem.RemarkType == remType && rem.UserName==userName
                         orderby rem.RemarkId descending
                         select new ProductsToRemarkModel
                         {
                             RemarkId = rem.RemarkId,
                             ProductId = rem.ProductId,
                             ProductName = prod.ProductName,
                             UserName = rem.UserName,
                             Body = rem.Body,
                             DateTime = rem.DateTime
                         }).Skip((page - 1) * pageSize).Take(pageSize).ToList();

            return query;
        }

        public IEnumerable<FProductList> GetProByKeyWord(ICriteria criteria, int page, int pageSize, Order orderExpr)
        {
            var products = orderExpr.OrderFrom<Product>(this.dataContext.CreateQuery<Product>("[Products]").Where(criteria.Expression, criteria.Parameters.Select(p => new ObjectParameter(p.Name, p.Value)).ToArray())).Skip((page - 1) * pageSize).Take(pageSize);

            var fProducts = (from pro in products
                            select new FProductList
                            {
                                ProductId = pro.ProductId, 
                                CategoryId=pro.CategoryId,
                                ProductName = pro.ProductName,
                                MarkePrice = pro.MarkePrice,
                                SellPrice = pro.SellPrice,
                                Stock = pro.Stock,
                                ImageAddress = (from img in this.dataContext.Product_Images
                                                where img.ProductId == pro.ProductId
                                                select img.ImageAddress).FirstOrDefault(),
                                Remark = (from rem in this.dataContext.Remarks
                                          where rem.ProductId == pro.ProductId
                                          select rem.RemarkId).Count()
                            });
                          

            return fProducts;
        }

        public IEnumerable<ProductList> GetProductList(ICriteria criteria, int page, int pageSize, Order orderExpr)
        {
            var products = orderExpr.OrderFrom<Product>(this.dataContext.CreateQuery<Product>("[Products]").Where(criteria.Expression, criteria.Parameters.Select(p => new ObjectParameter(p.Name, p.Value)).ToArray())).Skip((page - 1) * pageSize).Take(pageSize);

            var pList = (from pro in products
                         select new ProductList
                         {
                             ProductId = pro.ProductId,
                             CategoryId = pro.CategoryId,
                             ProductName = pro.ProductName,
                         }).ToList();

            return pList;
        }

        #endregion
    }
}
